package stores

import (
	"jvm/instructions/base"
	"jvm/rtda"
)

// ASTORE Store reference into local variable
type ASTORE struct{ base.Index8Instruction }

func (al *ASTORE) Execute(jFrame *rtda.JFrame) {
	_astore(jFrame, al.Index)
}

type ASTORE_0 struct{ base.NoOperandsInstruction }

func (al *ASTORE_0) Execute(jFrame *rtda.JFrame) {
	_astore(jFrame, 0)
}

type ASTORE_1 struct{ base.NoOperandsInstruction }

func (al *ASTORE_1) Execute(jFrame *rtda.JFrame) {
	_astore(jFrame, 1)
}

type ASTORE_2 struct{ base.NoOperandsInstruction }

func (al *ASTORE_2) Execute(jFrame *rtda.JFrame) {
	_astore(jFrame, 2)
}

type ASTORE_3 struct{ base.NoOperandsInstruction }

func (al *ASTORE_3) Execute(jFrame *rtda.JFrame) {
	_astore(jFrame, 3)
}

func _astore(jFrame *rtda.JFrame, index uint) {
	ref := jFrame.OperandStack().PopRef()
	jFrame.LocalVars().SetRef(index, ref)
}
